package com.tkjt.pms.server.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.common.entity.TkDepartGroup;
import com.tkjt.pms.common.entity.TkIndexInfo;
import com.tkjt.pms.common.utils.InfoAttrUtil;
import com.tkjt.pms.modules.common.mapper.TkDepartGroupMapper;
import com.tkjt.pms.modules.common.mapper.TkIndexInfoMapper;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartAndInfoTool {
    @Autowired
    private TkDepartGroupMapper tkDepartGroupMapper;
    @Autowired
    private TkIndexInfoMapper tkIndexInfoMapper;

    public List<String> getIndexCodesByParam(String orgCode,List<String> infoAttrs){
        List<TkIndexInfo> infos = getInfosByParam(orgCode,infoAttrs);
        List<String> list = new ArrayList<>();
        for(TkIndexInfo info : infos){
            list.add(info.getIndexCode());
        }
        return list;
    }

    public List<String> getIndexValueCodesByParam(String orgCode,List<String> infoAttrs){
        List<TkIndexInfo> infos = getInfosByParam(orgCode,infoAttrs);
        List<String> list = new ArrayList<>();
        for(TkIndexInfo info : infos){
            list.add(info.getIndexValueCode());
        }
        return list;
    }

    public List<TkIndexInfo> getInfosByParam(String orgCode,List<String> infoAttrs){
        Map<String,Object> param = new HashMap<>();
        if(StringUtils.isNotEmpty(orgCode)){
            QueryWrapper<TkDepartGroup> departGroupQueryWrapper = new QueryWrapper<>();
            departGroupQueryWrapper.lambda().eq(TkDepartGroup::getIndexDepart,orgCode);
            TkDepartGroup tkDepartGroup = tkDepartGroupMapper.selectOne(departGroupQueryWrapper);
            if(tkDepartGroup == null){
                return new ArrayList<TkIndexInfo>();
            }
            param.put("indexGroup",tkDepartGroup.getIndexGroupCode());
        }
        if(infoAttrs != null && infoAttrs.size() > 0){
            param.put("indexAttrs",infoAttrs);
        }
        param.put("orderAsc","orderAsc");
        List<TkIndexInfo> infos = tkIndexInfoMapper.selectByArrtsParam(param);
        return infos;
    }

    public List<String> getOrgCodesByOrgGroup(TkDepartGroup tkDepartGroup){
        QueryWrapper<TkDepartGroup> departGroupQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(tkDepartGroup.getIndexGroup())){
            departGroupQueryWrapper.lambda().eq(TkDepartGroup::getIndexGroup,tkDepartGroup.getIndexGroup());
        }
        if(StringUtils.isNotEmpty(tkDepartGroup.getIndexGroupCode())){
            departGroupQueryWrapper.lambda().eq(TkDepartGroup::getIndexGroupCode,tkDepartGroup.getIndexGroupCode());
        }
        if(StringUtils.isNotEmpty(tkDepartGroup.getDepartAttr())){
            departGroupQueryWrapper.lambda().eq(TkDepartGroup::getDepartAttr,tkDepartGroup.getDepartAttr());
        }
        List<TkDepartGroup> tkDepartGroups = tkDepartGroupMapper.selectList(departGroupQueryWrapper);
        List<String> orgCodes = new ArrayList<>();
        for (TkDepartGroup item : tkDepartGroups){
            orgCodes.add(item.getIndexDepart());
        }
        return orgCodes;
    }

    public List<String> getIndexCodesByOrgGroup(String orgGroup,List<String> infoAttrs){
        List<TkIndexInfo> infos = getInfosByOrgGroup(orgGroup,infoAttrs);
        List<String> list = new ArrayList<>();
        for(TkIndexInfo info : infos){
            list.add(info.getIndexCode());
        }
        return list;
    }

    public List<String> getIndexValueCodesByOrgGroup(String orgGroup,List<String> infoAttrs){
        List<TkIndexInfo> infos = getInfosByOrgGroup(orgGroup,infoAttrs);
        List<String> list = new ArrayList<>();
        for(TkIndexInfo info : infos){
            list.add(info.getIndexValueCode());
        }
        return list;
    }

    public List<TkIndexInfo> getInfosByOrgGroup(String orgGroup,List<String> infoAttrs){
        Map<String,Object> param = new HashMap<>();
        if(StringUtils.isNotEmpty(orgGroup)){
            param.put("indexGroup",orgGroup);
        }
        if(infoAttrs != null && infoAttrs.size() > 0){
            param.put("indexAttrs",infoAttrs);        }
        List<TkIndexInfo> infos = tkIndexInfoMapper.selectByArrtsParam(param);
        return infos;
    }
}
